import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService, LoginRes } from './auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super();
  }

  async validate(uName: string, uPws: string): Promise<LoginRes> {
    const loginRes = await this.authService.validateUser(uName, uPws);
    if (!loginRes) {
      throw new UnauthorizedException();
    }
    return loginRes;
  }
}
